package org.peoples.rooms;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import org.red5.server.api.IClient;
import org.red5.server.api.IConnection;

public class Room {
	
	private Set<IClient> clients;
	private RoomProperties properties;
	
	
	public Room(RoomProperties properties) {
		clients = new HashSet<IClient> ();
		this.properties = properties;
	}
	
	public Room(RoomProperties properties, Set<IClient> clients) {
		this.clients = clients;
		this.properties = properties;
	}
	
	public boolean addClient(IClient client) {
		return clients.add(client);
	}
	
	public boolean equals(Object obj) {
		if (obj == this)
			return true;
		
		if (! (obj instanceof Room))
			return false;
		
		Room temp_room = (Room) obj;
		if (properties.equals(temp_room.getRoomProperties()))
			return true;
		
		return false;
	}
	
	public int hashCode() {
		return properties.hashCode();
	}
	
	public Set<IClient> getClients() {
		return clients;
	}
	
	public Set<IConnection> getConnections() {
		Set<IConnection> connections = new HashSet<IConnection> ();
		Iterator<IClient> itr = clients.iterator();
		
		while (itr.hasNext()) {
			connections.addAll(itr.next().getConnections());
		}
		
		return connections;
	}
	
	public RoomProperties getRoomProperties() {
		return properties;
	}
	
	public boolean isEmpty() {
		return size() == 0;
	}
	
	public boolean removeClient(IClient client) {
		return clients.remove(client);
	}
	
	public int size() {
		return clients.size();
	}
	
	public String toString() {
		return properties.toString();
	}
	
}
